package j_util.graphics.gui;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
public class ScaleDialog extends JDialog {
    private javax.swing.JButton acceptButton;
    private javax.swing.JFormattedTextField textField;
    private javax.swing.JLabel titleLabel;

    public ScaleDialog() {
        initComponents();
    }

    private void initComponents() {

        titleLabel = new javax.swing.JLabel();
        textField = new javax.swing.JFormattedTextField();
        acceptButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        acceptButton.setText("Accept");
        acceptButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (textField.getText().equals("")) {
                    JOptionPane.showMessageDialog(ScaleDialog.this, "Please insert a scale value.");
                } else {
                    if (Double.parseDouble(textField.getText()) >= 1000 || Double.parseDouble(textField.getText()) <= 0.001) {
                        JOptionPane.showMessageDialog(ScaleDialog.this, "Scale value should be between \n 0._09 and 999.");
                    } else {
                        if (Double.parseDouble(textField.getText()) == 0) {
                            JOptionPane.showMessageDialog(ScaleDialog.this, "Scale value should be different from 0.");
                        }
                        dispose();
                    }
                }
            }
        });

        titleLabel.setText("Scale:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                        .addComponent(titleLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textField, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(acceptButton))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(titleLabel)
                        .addComponent(textField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(acceptButton))
        );
        setResizable(false);
        pack();
    }

    public double getScaleValue() {
        return Double.parseDouble(textField.getText());
    }

    public static void main(String[] args) {
        ScaleDialog scaleDialog = new ScaleDialog();
        scaleDialog.setVisible(true);
    }
}
